package cn.toolck.minitomcat.map;

import cn.toolck.minitomcat.container.Context;
import cn.toolck.minitomcat.container.Host;
import cn.toolck.minitomcat.container.Wapper;
import jdk.nashorn.internal.runtime.logging.Logger;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @ClassName Mapper
 * @Description 封装容器间的映射关系
 * @Author Toolck Rin
 * @Date 2021/3/17 23:23
 * @Version 1.0
 **/
public class Mapper {

    private List<MapperHost> mapperHosts = new ArrayList<>();


    public <T> int find(String url, List<T> mapperElements) {
        for (int i = 0; i < mapperElements.size(); i++) {
            T mapperElement = mapperElements.get(i);
            String name = null;
            try {
                Method getName = mapperElement.getClass().getMethod("getName");
                name = (String) getName.invoke(mapperElement, null);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (url.startsWith("/") && (!name.startsWith("/"))) {
                url = url.substring(1);
            }
            boolean startsWith = url.startsWith(name);
            if (name.equals(url)) {
                return i;
            }
            String substring = url.substring(name.length());
            boolean startsWith1 = substring.startsWith("/");
            if (startsWith && startsWith1) {
                return i;
            }
        }
        return -1;
    }

    public MappedContext find(String host, String url) {
        try {
            int i = find(host, mapperHosts);
            if (i < 0) {
                return null;
            }
            MapperHost mapperHost = mapperHosts.get(i);
            List<MappedContext> contexts = mapperHost.contexts;
            Integer j = find(url, contexts);
            if (j < 0) {
                System.out.println("不存在此资源：" + url);
                return null;
            }
            return contexts.get(j);
        } catch (Exception e) {
            System.out.println("不存在此资源：" + url);
        }
        return null;
    }


    public static class MapperElement<T> {
        private String name;
        private T obj;

        public MapperElement() {
        }

        public MapperElement(String name, T obj) {
            this.name = name;
            this.obj = obj;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public T getObj() {
            return obj;
        }

        public void setObj(T obj) {
            this.obj = obj;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            MapperElement<?> that = (MapperElement<?>) o;
            return Objects.equals(name, that.name) &&
                    Objects.equals(obj, that.obj);
        }

        @Override
        public int hashCode() {
            return Objects.hash(name, obj);
        }
    }

    public static class MapperHost extends MapperElement<Host> {
        public List<MappedContext> contexts = new ArrayList<>();
    }

    public static class MappedContext extends MapperElement<Context> {
        public List<MappedWapper> wappers = new ArrayList<>();
    }

    public static class MappedWapper extends MapperElement<Wapper> {
    }

    public List<MapperHost> getMapperHosts() {
        return mapperHosts;
    }

    public void setMapperHosts(List<MapperHost> mapperHosts) {
        this.mapperHosts = mapperHosts;
    }
}
